package com.iwhalecloud.bss.kite.cucc.service.listener.handler;

import com.iwhalecloud.bss.kite.client.vo.scene.ProdAttrVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProdRoleModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProductModuleVO;
import com.iwhalecloud.bss.kite.common.log.KiteLogger;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.CuccProdAttrVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Zhu
 * @Date: 2020/4/23
 */
@Service
public class DivideProdAttrHandler {

    @KiteLogger(isWrite = true)
    public void divideProdAttr(String sceneInstId, String custId, List<ProductModuleVO> productModuleVOList) {
        //通过dc_public获取分组的配置
        List<DcPublic> dcPublicList = DcPublicCache.get(LocalKeyConsts.GROUP_ATTR);

        for (ProductModuleVO productModuleVO : productModuleVOList) {
            List<ProdRoleModuleVO> prodRoleList = productModuleVO.getProdRole();
            for (ProdRoleModuleVO prodRoleModuleVO : prodRoleList) {
                //根据角色获取分组配置
                String roleId = prodRoleModuleVO.getProdRoleId();
                List<DcPublic> roleDcPublicList = dcPublicList.stream().filter(dcPublic->{
                    return dcPublic.getPkey().equals(roleId);
                }).collect(Collectors.toList());
                //获取产品对应的属性列表
                List<ProdAttrVO> prodAttrList = prodRoleModuleVO.getProdAttr().getProdAttrList();
                if (KiteListUtils.isNotEmpty(roleDcPublicList)) {
                    prodAttrList.forEach(prodAttrVO -> {
                        roleDcPublicList.forEach(dcPublic -> {
                            if (dcPublic.getCodea().toString().contains(prodAttrVO.getFieldName())) {
                                CuccProdAttrVO cuccProdAttrVO = (CuccProdAttrVO) prodAttrVO;
                                cuccProdAttrVO.setGroupName(dcPublic.getPname());
                                cuccProdAttrVO.setGroupId(dcPublic.getPcode());
                            }
                        });

                    });
                }
            }
        }
    }

}
